const app = getApp()

import Dialog from '@vant/weapp/dialog/dialog';
import {
    showMsg
} from '../../../../utils/config.js'

import {
    reqhiddenDangerupdate,
    reqMaintenanceget,
    reqworkOrderupdate
} from '../../../../request/api.js'
import {
    validateEmoji
} from '../../../../utils/validate.js'

Page({

    data: {
        navHeight: app.globalData.navHeight + 'px', //头部距离
        bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离
        info: {}, //告警信息

        remark: "",
        maintenanceId: "",
        maintenanceFlag: false, //控制维保单位
        showPeopleCall: false,
        maintenanceList: [],
        maintenanceUserId: "",
        maintenanceName: "",
        fileImageList: [],
        fileVideoList: [],
        radio:"通过",
        title:"处理工单"
    },

    onLoad(e) {
        this.setData({
            info: JSON.parse(e.info),
        })
        console.log(JSON.parse(e.info));
        let info=JSON.parse(e.info)
        if ( info.status== 1 ||  info.status== 2) {
            this.getMaintenanceget(JSON.parse(e.info))
        }
        if ( info.status== 4 ||  info.status== 5) {
            this.setData({
                title:"审核工单"
            })
        }else{
            this.setData({
                title:"处理工单"
            })
        }
        // wx.removeStorageSync('maintenanceId')
        // wx.removeStorageSync('maintenanceName')
    },
    async getMaintenanceget(info) {
        let res = await reqMaintenanceget(info.maintenanceId)
        if (res.code !== 20000) return showMsg(res.message)
        console.log(res);
        this.setData({
            maintenanceList: res.data.rows
        })
    },
    onShow() {
        // if (wx.getStorageSync('maintenanceId')) {
        //     console.log(wx.getStorageSync('maintenanceId'));
        //     this.setData({
        //         maintenanceId: wx.getStorageSync('maintenanceId'),
        //         maintenanceName: wx.getStorageSync('maintenanceName'),
        //     })
        // }
    },
    // onUnload() {
    //     wx.removeStorageSync('checkboxList')
    // },
    bindinput(e) {
        let remark = validateEmoji(e.detail.value.replace(/\s+/g, ''))
        if (remark.length > 100) {
            this.setData({
                remark: remark.substring(0, 100), //截取前200位
            })
            return;
        }
        this.setData({
            remark
        })
    },

    // 选择维保单位
    onChoseDevices() {
        wx.navigateTo({
            url: '/pages/message-index-page/hidden-maintenance/hidden-maintenance',
        })
    },
    async onSave(e) {
        console.log(e.currentTarget.dataset.status);
        let status=e.currentTarget.dataset.status
        // 派单 重新派单
        if ((this.data.info.status == 1 || this.data.info.status == 2) && this.data.maintenanceName == '') return showMsg('请选择维保技工')
        // 重新派单
        if(this.data.info.status == 2 && e.currentTarget.dataset.status==1){
            status=11;// 重新派单
        }
        let result = ''
        if (this.data.info.status == 1 || this.data.info.status == 2) {
            result = '派单'
        }
        // 修复提交 修复完成
        if (e.currentTarget.dataset.status==4) {
            result = '修复提交'
        }
        if (e.currentTarget.dataset.status==5) {
            result = '修复完成'
        }
        // 审核
        if (e.currentTarget.dataset.status==6) {
            if(this.data.info.status==4 && this.data.radio=='通过'){
                result = '通过'
                status=6
            }else if(this.data.info.status==4 && this.data.radio=='不通过'){
                result = '不通过'
                status=7
            }
            if(this.data.info.status==5 && this.data.radio=='通过'){
                result = '通过'
                status=8
            }else if(this.data.info.status==5 && this.data.radio=='不通过'){
                result = '不通过'
                status=9
            }
        }
        let picList=[]
        this.data.fileImageList.forEach(e=>{
            picList.push(e.url)
        })
        let videoFile=''
        this.data.fileVideoList.forEach(e=>{
            videoFile=e.url
        })
        let res = await reqworkOrderupdate({
            id: this.data.info.id,
            userId: app.globalData.userId,
            username: app.globalData.username,
            status: status,
            result: result,
            remark: this.data.remark,
            maintenanceUserId: this.data.maintenanceUserId,
            picList: picList,
            videoFile: videoFile
        })
        if (res.code !== 20000) return showMsg(res.message)
        showMsg("处理成功", "success")
        setTimeout(() => {
            wx.navigateBack({
                delta: 1,
            })
        }, 1000)
    },
    // 点击隐藏弹窗
    closePeopleCall() {
        this.setData({
            showPeopleCall: false,
        })
    },
    // 点击展示弹窗
    clickShowPeopleCall(e) {
        this.setData({
            showPeopleCall: true,
        })
    },
    // 点击获取alarmReceiverArr的值
    deviceToggle(event) {
        console.log(event);
        const {
            id
        } = event.currentTarget.dataset;
        console.log(id);
        this.setData({
            maintenanceUserId: id
        });
    },
    onChange(event) {
        console.log(event.detail);
        this.setData({
            maintenanceUserId: event.detail,
        });
    },
    onradio(event) {
        console.log(event.detail);
        this.setData({
            radio: event.detail,
        });
    },
    //   确定弹窗
    confirmPeopleCall() {
        let maintenanceName = ''
        this.data.maintenanceList.forEach(item => {
            if (item.userId == this.data.maintenanceUserId) {
                maintenanceName = item.username
            }
        })
        this.setData({
            maintenanceName,
            showPeopleCall: false
        });
    },
      // 删除照片
      deletedFile(e) {
        let type = e.currentTarget.dataset.type
        if (type == 'image') {
            this.data.fileImageList.splice(e.detail.index, 1)
            this.setData({
                fileImageList: this.data.fileImageList
            })
        } else if (type == 'video') {
            this.data.fileVideoList.splice(e.detail.index, 1)
            this.setData({
                fileVideoList: this.data.fileVideoList
            })
        }
    },
    beforeRead(event) {
        console.log(event, "event");
        const {
            file,
            callback
        } = event.detail;
        callback(file.type === 'image' || file.type === 'video');
    },
      // 上传照片
      async afterRead(e) {
        console.log(e, 'e');
        let _this = this;
        let file = e.detail.file.url
        let type = e.currentTarget.dataset.type
        wx.uploadFile({
            url: app.globalData.reqUrl + 'file/upload',
            filePath: file,
            name: 'file',
            header: {
                "Content-Type": "multipart/form-data"
            },
            formData: {
                "token": app.globalData.token
            },
            success: function (res) {
                var res = JSON.parse(res.data)
                console.log(res);
                if (res.code == 20000) {
                    if (type == 'image') {
                        _this.data.fileImageList.push({
                            url: res.data.url,
                        })
                        _this.setData({
                            fileImageList: _this.data.fileImageList
                        })
                    } else if (type == 'video') {
                        _this.data.fileVideoList.push({
                            url: res.data.url,
                        })
                        _this.setData({
                            fileVideoList: _this.data.fileVideoList
                        })
                    }
                } else if (res.statusCode != 20000) {
                    wx.showModal({
                        title: '提示',
                        content: '上传失败',
                        showCancel: false
                    })
                    return;
                }
            },
            fail: function (e) {
                wx.showModal({
                    title: '提示',
                    content: '上传失败',
                    showCancel: false
                })
            }
        })
    },
})